package com.net.oil.product.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.http.AppUrl;
import com.net.oil.product.bean.ProductClassBean;
import com.net.oil.util.StringUtil;
import com.net.oil.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2018/4/5:17:21
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class ProductClassAdapter extends RecyclerView.Adapter<ProductClassAdapter.ViewHolder> {


    private Context context;
    private LayoutInflater inflater;

    private List<ProductClassBean> productClassBeanList;
    private OnItemClickListener onItemClickListener;

    public ProductClassAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        productClassBeanList = new ArrayList<>();
    }

    public void updateData(List<ProductClassBean> productClassBeanList){
        this.productClassBeanList.addAll(productClassBeanList);
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.product_class_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final ProductClassBean productClassBean = productClassBeanList.get(position);
        if (productClassBean != null){
            if (productClassBean.getCatName() != null){
                holder.productClassName.setText(productClassBean.getCatName());
            }
            if (!StringUtil.isEmpty(productClassBean.getImage())){
                GlideUtil.getInstance().loadImage(context, holder.productClassLogo, AppUrl.getImageUrl(productClassBean.getImage()),
                        R.drawable.product_list_err);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onClick(productClassBean.getCatId());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return productClassBeanList == null ? 0 : productClassBeanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.product_class_logo)
        ImageView productClassLogo;
        @BindView(R.id.product_class_name)
        TextView productClassName;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener{
        void onClick(int id);
    }
}
